import array
import collections
import time
import gc
import math
import cStringIO



def measure(runner, *args):
	gc.collect()
	start = time.time()
	runner(*args)
	stop = time.time()
	gc.collect()
	elapsed = stop - start
	return elapsed

class StringQueue:
	def __init__(self):
		self.buf = ""
	def __len__(self):
		return len(self.buf)
	def __getitem__(self, k):
		return self.buf[k]
	def pop(self):
		ch = self.buf[0]
		self.buf = self.buf[1:]
		return ch
	def append(self, ch):
		self.buf += ch

def index(data, loops):
	antiomit = None
	for i in xrange(len(data)):
		antiomit = data[i]
def enque(data, loops):
	antiomit = None
	for i in xrange(loops):
		data.append(' ')
def dequeList(data, loops):
	antiomit = None
	for i in xrange(loops):
		antiomit = data.pop(0)
def dequeQueue(data, loops):
	antiomit = None
	for i in xrange(loops):
		antiomit = data.pop()

def record(methods, data, length):
	sbuf = cStringIO.StringIO()
	sbuf.write( data.__class__.__name__ )
	for mth in methods:
		sbuf.write( ", %07.3e" % measure(mth, data, length) )
	return sbuf.getvalue()

def testCollections(length):
	methodsList = [ enque, index, dequeList ]
	methodsQueue = [ enque, index, dequeQueue ]
	
	print record(methodsQueue, StringQueue(), length)
	print record(methodsList, [], length)
	print record(methodsList, array.array('c'), length)
	print record(methodsQueue, collections.deque(), length)

def main():
	lengths = [10000 * 2 ** n for n in xrange(5) ]
	for len in lengths:
		print "len=%d" % len
		testCollections(len)

if __name__ == '__main__':
	main()

